package com.example.demo.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.HttpUtil;

import com.example.demo.service.Bob;

import com.example.demo.service.ProxyReEncrypt;
import com.example.demo.service.PubParams;
import com.example.demo.service.SystemProperty;

import it.unisa.dia.gas.jpbc.Element;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@RestController
public class BobController {
	
	static Bob b;
	@RequestMapping(value = "/bob/join", method = { RequestMethod.GET })//发送请求获得私钥
	public  void getBob() {
		try {
			String response=HttpUtil.sendGet("http://localhost:8082/rkGen","UTF-8");
			JSONArray ja = JSONArray.fromObject(response);
			ProxyReEncrypt  d=new ProxyReEncrypt ();
			PubParams p=null;
			try {
				p=d.read();// IO
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			int m=SystemProperty.getIntegerValue("m");
			int t=SystemProperty.getIntegerValue("t");
			Element id=p.Zr.newElementFromBytes(ja.getString(0).getBytes("ISO-8859-1"));
			Element sk1=p.G1.newElementFromBytes(ja.getString(1).getBytes("ISO-8859-1"));
			Element sk2=p.G2.newElementFromBytes(ja.getString(2).getBytes("ISO-8859-1"));
			b=new Bob(p,t,id,sk1,sk2);
			return ; 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ;
	}
	@RequestMapping(value = "/bob/download", method = { RequestMethod.GET })//发送请求获得私钥
	public String decryptFile() {
		b.getEncryptedMessage();
		Element A=b.getA();
		try {
			b.getRekey();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return b.dec(A);
		
	}

}
